package org.scirp.ijmis.dao.personpaper.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.scirp.ijmis.dao.base.impl.BaseDAOImpl;
import org.scirp.ijmis.dao.personpaper.ILoginDAO;
import org.scirp.ijmis.po.personpaper.Login;
import org.scirp.ijmis.po.personpaper.Person;
import org.scirp.ijmis.util.Pagination;

public class LoginDAOImpl extends BaseDAOImpl<Login> implements ILoginDAO {

	private static final String className = LoginDAOImpl.class.toString();

	public List<Login> selectLogin(String loginName, String MD5Password, long journalID) {
		String hql = "from Login where loginName=:loginName and  md5Password=:md5Password and person.journal.pid=:journalID and isActive='true'";

		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("loginName", loginName);
		parameters.put("md5Password", MD5Password);
		parameters.put("journalID", journalID);

		return super.ExecuteQuery(hql, parameters, className + "-->selectLogin(String loginName, String MD5Password)");
	}

	@Override
	public boolean selectLogin(String loginName, String MD5Password) {
		String hql = "select count(*) from Login where loginName=:loginName and  md5Password=:md5Password  and isActive='true'";
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("loginName", loginName);
		parameters.put("md5Password", MD5Password);
		Object result = super.selectSingle(hql, parameters, className + "-->selectLogin(String loginName, String MD5Password)");
		return Integer.parseInt(result.toString()) > 0 ? true : false;
	}

	public Long selectCountByRoleType(long roleId, long journalID) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		String hql = "select count(pid) from Login where 1=1 ";
		if (roleId != 0) {
			parameters.put("roleId", roleId);
			hql += " and person.roleType.pid=:roleId ";
		}
		if (journalID != 0) {
			parameters.put("journalID", journalID);
			hql += "and person.journal.pid=:journalID ";
		}
		return Long.parseLong(selectSingle(hql, parameters, className + "-->selectCountByRoleType(long roleId, long journalID)").toString());
	}

	public List<Login> selectLoginByRoleType(long roleId, long journalID, Pagination pagination) {
		Map<String, Object> parameters = new HashMap<String, Object>();
		String hql = "from Login where 1=1 ";
		if (roleId != 0) {
			parameters.put("roleId", roleId);
			hql += " and person.roleType.pid=:roleId ";
		}
		if (journalID != 0) {
			parameters.put("journalID", journalID);
			hql += "and person.journal.pid=:journalID ";
		}
		hql += " order by registerDateTime desc ";
		if (pagination != null) {
			return super.select(hql, parameters, pagination.getCurrentPage(), pagination.getPageSize(), className + "-->selectLoginByRoleType(long roleId, long journalID)");
		} else {
			return super.select(hql, parameters, 0,0,className + "-->selectLoginByRoleType(long roleId, long journalID)");
		}
	}

	public Login selectByPersonId(Long personId) {
		String hql = "from Login where person.pid=:personId";

		Map<String, Long> parameters = new HashMap<String, Long>();
		parameters.put("personId", personId);

		List<Login> list = super.ExecuteQuery(hql, parameters, className + "-->selectByPersonId(Long personId)");

		if (list != null && list.size() > 0) {
			return list.get(0);
		}

		return null;

	}

	@Override
	public Login selectExpertByEmailAndJournal(String email, Long journalId) {
		String hql = "from Login where person.email1=:email and person.roleType.pid=:roleId and person.journal.pid=:journalId";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("email", email);
		map.put("journalId", journalId);
		map.put("roleId", 8L);
		List<Login> logins=super.ExecuteQuery(hql, map, className + "-->selectExpertByEmailAndJournal(String email, Long journalId)");
		return  logins!=null&&logins.size()>0?logins.get(0):null;
	}

	@SuppressWarnings("unchecked")
	public Boolean passwordModification(Long personId, String plainPassword, String MD5Password) {
		String hql = "update Login set plainPassword=:plainPassword, md5Password=:md5Password where person.pid=:personId";

		Map parameters = new HashMap();
		parameters.put("plainPassword", plainPassword);
		parameters.put("md5Password", MD5Password);
		parameters.put("personId", personId);

		return super.ExecuteNonQuery(hql, parameters, className + "-->passwordModification(Long personId, String plainPassword,String MD5Password) ");
	}

	public List<Login> selectAll() {

		String hql = "from Login order by pid desc";
		return super.ExecuteQuery(hql, className + "-->selectAll");
	}

	public Long countObject() {
		return super.countObject(className + "-->selectAll");
	}

	public Login selectById(Object id) {

		return (Login) super.selectById(id, className + "-->selectById");
	}

	public long saveLogin(Login entity) {
		return super.saveObject(entity, className + "-->saveLogin(Login entity)");
	}

	public boolean updateLogin(Login entity) {
		return super.updateObject(entity, className + "-->updateLogin(Login entity)");
	}

	public Login selectLoginByLoginName(String loginName) {
		String hql = "from Login where loginname=:loginName";
		Map<String, String> map = new HashMap<String, String>();
		map.put("loginName", loginName);
		return (Login) super.selectSingle(hql, map, className + "-->selectLoginByLoginName(String loginName)");
	}

	public Login deleteLogin(Login entity) {
		return super.deleteObject(entity, className + "-->deleteLogin(Login entity)");
	}

	public boolean deleteLoginByIds(Object[] idCollection) {
		return super.deleteObject(idCollection, className + "-->deleteLoginByIds(Object[] idCollection)");
	}

	public Login selectLoginByLoginNameAndEmail(String loginName, String email) {
		String hql = "from Login where loginname=:loginName and person.email1=:email or person.email2=:email";
		Map<String, String> map = new HashMap<String, String>();
		map.put("loginName", loginName);
		map.put("email", email);
		return (Login) super.selectSingle(hql, map, className + "-->selectLoginByLoginNameAndEmail(String loginName,String email)");

	}

	public Login selectLoginByLoginNameAndEmail(String loginName, String email, String answer) {
		String hql = "from Login where loginname=:loginName and answer=:answer and person.email1=:email or person.email2=:email";
		Map<String, String> map = new HashMap<String, String>();
		map.put("loginName", loginName);
		map.put("answer", answer);
		map.put("email", email);
		return (Login) super.selectSingle(hql, map, className + "-->selectLoginByLoginNameAndEmail(String loginName,String email)");

	}

	public Login selectLoginByLoginNameAndIdentifier(String loginName, String identifier) {
		String hql = "from Login where loginName=:loginName and identifier=:identifier";
		Map<String, String> parameter = new HashMap<String, String>();
		parameter.put("loginName", loginName);
		parameter.put("identifier", identifier);
		return (Login) super.selectSingle(hql, parameter, className + "selectLoginByLoginNameAndIdentifier(String loginName)");
	}

	public List<Login> selectByCondition(long expertSourcesId, long researchFieldId, long journalID, long paperNo, String email, Pagination pagination) {
		String sql = "select * from t_login where ";
		
		Map<String, Object> map = new HashMap<String, Object>();
		if (researchFieldId > 0) {
			sql += "  personid in(select personid from T_RESEARCHFIELDOFPERSON where researchfieldid=:researchFieldId ) and";
			map.put("researchFieldId", researchFieldId);
		}
		sql+=" personid in(select pid from t_person where roleid=8";
		if (expertSourcesId > 0) {
			sql += " and expertsourcesid=:expertSourcesId";
			map.put("expertSourcesId", expertSourcesId);
		}
		if(journalID>0){
			sql+=" and  journalid=:journalID";
			map.put("journalID", journalID);
		}
		
		if (paperNo > 0) {
			sql += " and pid in(select personid from T_REVIEWPAPER where paperid in(select pid from t_paper where paperno=:paperNo))";
			map.put("paperNo", paperNo);
		}
		if (email != null && !email.trim().equals("")) {
			sql += " and (lower(email1) like :email or lower(email2) like :email or lower(replace(firstname||lastname,' ','')) like :email)";
			map.put("email", "%"+email.toLowerCase().trim().replace(" ", "")+"%");
		}
		sql += ") order by registerDateTime desc";
		if (pagination != null) {
			return super.selectBySql(sql,map,pagination.getCurrentPage(),pagination.getPageSize(), className + "selectByCondition(long expertSourcesId,long researchFieldId,long paperNo,String email,Pagination pagination)");
		}else{
			return super.selectBySql(sql,map, 0, 0, className + "selectByCondition(long expertSourcesId,long researchFieldId,long paperNo,String email,Pagination pagination)");

		}
	}

	public Long selectCountByCondition(long expertSourcesId, long researchFieldId,long journalID, long paperNo, String email) {
		String sql = "select count(*) from t_login where personid in(select pid from t_person where roleid=8";
		Map<String, Object> map = new HashMap<String, Object>();
		if (expertSourcesId > 0) {
			sql += " and expertsourcesid=:expertSourcesId";
			map.put("expertSourcesId", expertSourcesId);
		}
		if(journalID>0){
			sql+=" and  journalid=:journalID";
			map.put("journalID", journalID);
		}
		if (researchFieldId > 0) {
			sql += " and pid in(select personid from T_RESEARCHFIELDOFPERSON where researchfieldid=:researchFieldId )";
			map.put("researchFieldId", researchFieldId);
		}
		if (paperNo > 0) {
			sql += " and pid in(select personid from T_REVIEWPAPER where paperid in(select pid from t_paper where paperno=:paperNo))";
			map.put("paperNo", paperNo);
		}
		if (email != null && !email.trim().equals("")) {
			sql += " and (lower(email1) like :email or lower(email2) like :email or lower(replace(firstname||lastname,' ','')) like :email)";
			map.put("email", "%"+email.toLowerCase().trim().replace(" ", "")+"%");
			
		}
		sql += ")";
		return super.selectCountBySql(sql,map, className + "selectCountByCondition(long expertSourcesId, long researchFieldId, long paperNo, String email)");
	}

}
